//
//  BN_PersonalModifyTextInfo.m
//  BabyNow
//
//  Created by 刘泽琛 on 14-5-19.
//  Copyright (c) 2014年 iMobile. All rights reserved.
//

#import "BN_PersonalModifyTextInfo.h"
#import "BN_UserHelper.h"

@implementation BN_PersonalModifyTextInfo {
    BN_UserInfo *userInfo;
}

- (void)awakeFromNib
{
    //UI
    self.contentView.backgroundColor = [UIColor whiteColor];
    self.fieldsName.font = [UIFont fontWithName:@"TrebuchetMS-Bold" size:16];
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(hideKeyBoard:) name:@"KTEXTINFO_HIDEKEYBOARD" object:nil];
    
    self.fieldsInfoTextField.delegate = self;
    self.fieldsInfoTextField.returnKeyType = UIReturnKeyDone;
    
    // 取消键盘
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(cancelKeyBoard) name:@"K_MINE_PERSON_SETTING_DOWN_KEYBOARD" object:nil];
}

- (void)hideKeyBoard:(NSNotification *)notification {
    [self.fieldsInfoTextField resignFirstResponder];
}

- (void)textFieldDidBeginEditing:(UITextField *)textField {
    NSString *tagStr = [NSString stringWithFormat:@"%ld",(long)self.tag];
    [[NSNotificationCenter defaultCenter]postNotificationName:@"KMINE_CHANGE_PERSONAL_TABLEVIEW_HEIGHT" object:tagStr];
}

#define kMaxLength 13
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    
    NSString * toBeString = [textField.text stringByReplacingCharactersInRange:range withString:string];
    if (toBeString.length > kMaxLength && range.length!=1){
        textField.text = [toBeString substringToIndex:kMaxLength];
        [SVProgressHUD showErrorWithStatus:@"最多输入13个字哦"];
        return NO;
    }
    return YES;
}

// return方式保存信息
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    
    [self.fieldsInfoTextField resignFirstResponder];
    [[NSNotificationCenter defaultCenter]postNotificationName:@"K_MINE_TABLEVIEW_FRAME_RECOVERY" object:nil];
    
    userInfo = [BN_UserHelper unArchiverAccountInfo];
    self.userType = [userInfo.userType integerValue];
    BN_PersonModifyModel *personModifyModel = [BN_PersonModifyModel sharedModifyModel];
    switch (self.userType) {
        case ENUM_ROLE_LEADER:
        {
            switch (self.tag) {
                case 0:
                    break;
                case 1:
                    personModifyModel.nickName = textField.text;
                    break;
                case 2:
                    personModifyModel.biyeSchoolName = textField.text;
                    break;
                case 3:
                    personModifyModel.xueLiName = textField.text;
                    break;
                case 4:
                    personModifyModel.telPName = textField.text;
                    break;
                case 5:
                    personModifyModel.signName = textField.text;
                default:
                    break;
            }
        }
            break;

        case ENUM_ROLE_CLASS_TEACHER:
        {
            switch (self.tag) {
                case 0:
                    break;
                case 1:
                    personModifyModel.nickName = textField.text;
                    break;
                case 2:
                    personModifyModel.biyeSchoolName = textField.text;
                    break;
                case 3:
                    personModifyModel.xueLiName = textField.text;
                    break;
                case 4:
                    personModifyModel.telPName = textField.text;
                    break;
                case 5:
                    personModifyModel.signName = textField.text;
                default:
                    break;
            }
        }
            break;
        case ENUM_ROLE_PARENT:
        {
            switch (self.tag) {
                case 0:
                    break;
                case 1:
                    personModifyModel.nickName = textField.text;
                    break;
                case 2:
                    personModifyModel.babyBirtName = textField.text;
                    break;
                case 3:
                    personModifyModel.telPName = textField.text;
                    break;
                default:
                    break;
            }
        }
            break;
        default:
            break;
    }
    return YES;
}

- (void)cancelKeyBoard {
    [self.fieldsInfoTextField resignFirstResponder];
}

#pragma mark 资源
- (void)dealloc
{
    [[NSNotificationCenter defaultCenter]removeObserver:self name:@"KMINE_CHANGE_PERSONAL_TABLEVIEW_HEIGHT" object:nil];
    [[NSNotificationCenter defaultCenter]removeObserver:self name:@"K_MINE_TABLEVIEW_FRAME_RECOVERY" object:nil];

    self.fieldsInfoTextField.delegate = nil;
    self.fieldsInfoTextField = nil;
    
    NSLog(@"BN_PersonalModifyTextInfo 释放...");
}
@end
